package cn.seapon.talker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.seapon.talker.infocarrier.SharedItems;
import cn.seapon.talker.net.HttpRequest;

public class RegisterFinishActivity extends Activity {
	private static final String TAG = "||__RegisterFinishActivity__||";
	private static final String RegUrl = "http://1.lichtung.sinaapp.com/appserver/appservice.php?request_code=2002";

	private static ArrayList<HashMap<String, Object>> avatarsArrayList = new ArrayList<HashMap<String, Object>>();
	private GridView gridview;
	private ImageView iv_avatar;
	private String avartar_set = "avatar_252";// 默认是流氓兔
	private Button register_finished;

	private Bundle prev;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				register_finished.setOnClickListener(backToLoginListener);
				register_finished.setText("返回登录");
				break;
			case -1:
				Toast.makeText(RegisterFinishActivity.this, "注册失败了",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};
	
	private OnClickListener backToLoginListener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(RegisterFinishActivity.this,
					LoginActivity.class);
			intent.putExtra("account_name", prev.getString("account_name"));
			intent.putExtra("pwd", prev.getString("pwd"));
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);
		}
		
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题
		setContentView(R.layout.activity_register_finish);
		gridview = (GridView) findViewById(R.id.gv_avatars);
		iv_avatar = (ImageView) findViewById(R.id.iv_avatar);
		register_finished = (Button) findViewById(R.id.register_finished);

		prev = getIntent().getExtras();

		initResource();

		SimpleAdapter sa = new SimpleAdapter(this, avatarsArrayList,
				R.layout.avatar_item, new String[] { "id", "id", "name" },
				new int[] { R.id.ItemImage, R.id.ItemId, R.id.ItemName });
		gridview.setAdapter(sa);

		// 设置事件监听
		register_finished.setOnClickListener(listener);
		gridview.setOnItemClickListener(new ItemClickListener());
	}

	class ItemClickListener implements OnItemClickListener {
		/**
		 * 点击项时触发事件
		 * 
		 * @param parent
		 *            发生点击动作的AdapterView
		 * @param view
		 *            在AdapterView中被点击的视图(它是由adapter提供的一个视图)。
		 * @param position
		 *            视图在adapter中的位置。
		 * @param rowid
		 *            被点击元素的行id。
		 */
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long rowid) {
			TextView tv_name = (TextView) view.findViewById(R.id.ItemName);
			String avatar_name = tv_name.getText().toString().trim();
			TextView tv_id = (TextView) view.findViewById(R.id.ItemId);
			String avatar_id = tv_id.getText().toString().trim();
			// Toast.makeText(RegisterFinishActivity.this,
			// "你点击的是："+resourceId+"   图片名字是:"+text, Toast.LENGTH_SHORT).show();
			// iv_avatar.setImageResource(resourceId);
			iv_avatar.setImageResource(Integer.valueOf(avatar_id));
			Toast.makeText(RegisterFinishActivity.this,  avatar_name+" "+avatar_id , Toast.LENGTH_SHORT).show();
			avartar_set = avatar_name;
		}
	}

	private View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.register_finished:
				prev.putString("avatar", avartar_set);
				// Log.e(TAG, prev.getString("account_name"));
				// Log.e(TAG, prev.getString("pwd"));
				// Log.e(TAG, prev.getString("phone"));
				// Log.e(TAG, prev.getString("email"));
				// Log.e(TAG, prev.getString("signature"));
				// Log.e(TAG, prev.getString("nickname"));
				// Log.e(TAG, prev.getString("avatar"));
				StringBuilder builder = new StringBuilder();
				builder.append("&account_name="+avartar_set);
				builder.append("&pwd=" + prev.getString("pwd"));
				builder.append("&phone=" + prev.getString("phone"));
				builder.append("&email=" + prev.getString("email"));
				builder.append("&signature=" + prev.getString("signature"));
				builder.append("&nickname=" + prev.getString("nickname"));
				builder.append("&avatar=" + prev.getString("avatar"));
				final String url = RegUrl + builder.toString();
				new Thread() {

					@Override
					public void run() {
						Message message = Message.obtain();
						String retString = HttpRequest.doGetRequest(url);
						if ("success".equals(retString)) {
							message.what = 1;
							handler.sendMessage(message);
						} else {
							message.what = -1;
							handler.sendMessage(message);
						}
					}

				}.start();

				break;

			default:
				break;
			}
		}
	};




	/**
	 * 加载表情资源到程序中
	 */
	public static void initResource() {
		Set<String> keyset = SharedItems.avatarMap.keySet();
		String[] keys = keyset.toArray(new String[keyset.size()]);
		for (int i = 0; i < keys.length; i++) {
			HashMap<String, Object> avatar = new HashMap<String, Object>();
			avatar.put("id", SharedItems.avatarMap.get(keys[i]));
			avatar.put("name",keys[i] );
			avatarsArrayList.add(avatar);
		}
	}

}
